package com.zengqingfa.juc.n4;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;

/**
 * @fileName: Test6
 * @author: zengqf3
 * @date: 2021-6-10 11:11
 * @description:
 */
@Slf4j(topic = "c.Test4")
public class Test6 {

    public final void method1(int loopNumber) {
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < loopNumber; i++) {
            method2(list);
            method3(list);
        }
    }

    public void method2(ArrayList<String> list) {
        list.add(Thread.currentThread().getName() + "-1");
    }

    public void method3(ArrayList<String> list) {
        list.remove(0);
    }

    static final int THREAD_NUMBER = 2;
    static final int LOOP_NUMBER = 200;

}

class ThreadSafeSubClass extends Test6 {
    @Override
    public void method3(ArrayList<String> list) {
        new Thread(() -> {
            list.remove(0);
        }).start();
    }


    public static void main(String[] args) {
        ThreadSafeSubClass test = new ThreadSafeSubClass();
        for (int i = 0; i < THREAD_NUMBER; i++) {
            new Thread(() -> {
                test.method1(LOOP_NUMBER);
            }, "Thread" + i).start();
        }
    }
}